home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / GUSI-RPC 4.0 / clnt_perror.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-17  |  6.2 KB  |  294 lines  |  [TEXT/MPS ]

  1. /* @(#)clnt_perror.c    2.1 88/07/29 4.0 RPCSRC */
  2. /*
  3.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  4.  * unrestricted use provided that this legend is included on all tape
  5.  * media and as a part of the software program in whole or part.  Users
  6.  * may copy or modify Sun RPC without charge, but are not authorized
  7.  * to license or distribute it to anyone else except as part of a product or
  8.  * program developed by the user.
  9.  * 
  10.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  11.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  12.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  13.  * 
  14.  * Sun RPC is provided with no support and without any obligation on the
  15.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  16.  * modification or enhancement.
  17.  * 
  18.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  19.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  20.  * OR ANY PART THEREOF.
  21.  * 
  22.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  23.  * or profits or other special, indirect and consequential damages, even if
  24.  * Sun has been advised of the possibility of such damages.
  25.  * 
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30. #if !defined(lint) && defined(SCCSIDS)
  31. static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
  32. #endif
  33.  
  34. /*
  35.  * clnt_perror.c
  36.  *
  37.  * Copyright (C) 1984, Sun Microsystems, Inc.
  38.  *
  39.  */
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include <rpc/types.h>
  43. #include <rpc/auth.h>
  44. #include <rpc/clnt.h>
  45.  
  46. static char *auth_errmsg();
  47. extern char *strcpy();
  48.  
  49. static char *buf;
  50.  
  51. static char *
  52. _buf()
  53. {
  54.  
  55.     if (buf == 0)
  56.         buf = (char *)malloc(256);
  57.     return (buf);
  58. }
  59.  
  60. /*
  61.  * Print reply error info
  62.  */
  63. char *
  64. clnt_sperror(rpch, s)
  65.     CLIENT *rpch;
  66.     char *s;
  67. {
  68.     struct rpc_err e;
  69.     void clnt_perrno();
  70.     char *err;
  71.     char *str = _buf();
  72.     char *strstart = str;
  73.  
  74.     if (str == 0)
  75.         return (0);
  76.     CLNT_GETERR(rpch, &e);
  77.  
  78.     (void) sprintf(str, "%s: ", s);  
  79.     str += strlen(str);
  80.  
  81.     (void) strcpy(str, clnt_sperrno(e.re_status));  
  82.     str += strlen(str);
  83.  
  84.     switch (e.re_status) {
  85.     case RPC_SUCCESS:
  86.     case RPC_CANTENCODEARGS:
  87.     case RPC_CANTDECODERES:
  88.     case RPC_TIMEDOUT:     
  89.     case RPC_PROGUNAVAIL:
  90.     case RPC_PROCUNAVAIL:
  91.     case RPC_CANTDECODEARGS:
  92.     case RPC_SYSTEMERROR:
  93.     case RPC_UNKNOWNHOST:
  94.     case RPC_UNKNOWNPROTO:
  95.     case RPC_PMAPFAILURE:
  96.     case RPC_PROGNOTREGISTERED:
  97.     case RPC_FAILED:
  98.         break;
  99.  
  100.     case RPC_CANTSEND:
  101.     case RPC_CANTRECV:
  102.         (void) sprintf(str, "; errno = %s", strerror(e.re_errno));
  103.         str += strlen(str);
  104.         break;
  105.  
  106.     case RPC_VERSMISMATCH:
  107.         (void) sprintf(str,
  108.             "; low version = %lu, high version = %lu", 
  109.             e.re_vers.low, e.re_vers.high);
  110.         str += strlen(str);
  111.         break;
  112.  
  113.     case RPC_AUTHERROR:
  114.         err = auth_errmsg(e.re_why);
  115.         (void) sprintf(str,"; why = ");
  116.         str += strlen(str);
  117.         if (err != NULL) {
  118.             (void) sprintf(str, "%s",err);
  119.         } else {
  120.             (void) sprintf(str,
  121.                 "(unknown authentication error - %d)",
  122.                 (int) e.re_why);
  123.         }
  124.         str += strlen(str);
  125.         break;
  126.  
  127.     case RPC_PROGVERSMISMATCH:
  128.         (void) sprintf(str, 
  129.             "; low version = %lu, high version = %lu", 
  130.             e.re_vers.low, e.re_vers.high);
  131.         str += strlen(str);
  132.         break;
  133.  
  134.     default:    /* unknown */
  135.         (void) sprintf(str, 
  136.             "; s1 = %lu, s2 = %lu", 
  137.             e.re_lb.s1, e.re_lb.s2);
  138.         str += strlen(str);
  139.         break;
  140.     }
  141.     (void) sprintf(str, "\n");
  142.     return(strstart) ;
  143. }
  144.  
  145. void
  146. clnt_perror(rpch, s)
  147.     CLIENT *rpch;
  148.     char *s;
  149. {
  150.     (void) fprintf(stderr,"%s",clnt_sperror(rpch,s));
  151. }
  152.  
  153.  
  154. struct rpc_errtab {
  155.     enum clnt_stat status;
  156.     char *message;
  157. };
  158.  
  159. static struct rpc_errtab  rpc_errlist[] = {
  160.     { RPC_SUCCESS, 
  161.         "RPC: Success" }, 
  162.     { RPC_CANTENCODEARGS, 
  163.         "RPC: Can't encode arguments" },
  164.     { RPC_CANTDECODERES, 
  165.         "RPC: Can't decode result" },
  166.     { RPC_CANTSEND, 
  167.         "RPC: Unable to send" },
  168.     { RPC_CANTRECV, 
  169.         "RPC: Unable to receive" },
  170.     { RPC_TIMEDOUT, 
  171.         "RPC: Timed out" },
  172.     { RPC_VERSMISMATCH, 
  173.         "RPC: Incompatible versions of RPC" },
  174.     { RPC_AUTHERROR, 
  175.         "RPC: Authentication error" },
  176.     { RPC_PROGUNAVAIL, 
  177.         "RPC: Program unavailable" },
  178.     { RPC_PROGVERSMISMATCH, 
  179.         "RPC: Program/version mismatch" },
  180.     { RPC_PROCUNAVAIL, 
  181.         "RPC: Procedure unavailable" },
  182.     { RPC_CANTDECODEARGS, 
  183.         "RPC: Server can't decode arguments" },
  184.     { RPC_SYSTEMERROR, 
  185.         "RPC: Remote system error" },
  186.     { RPC_UNKNOWNHOST, 
  187.         "RPC: Unknown host" },
  188.     { RPC_UNKNOWNPROTO,
  189.         "RPC: Unknown protocol" },
  190.     { RPC_PMAPFAILURE, 
  191.         "RPC: Port mapper failure" },
  192.     { RPC_PROGNOTREGISTERED, 
  193.         "RPC: Program not registered"},
  194.     { RPC_FAILED, 
  195.         "RPC: Failed (unspecified error)"}
  196. };
  197.  
  198.  
  199. /*
  200.  * This interface for use by clntrpc
  201.  */
  202. char *
  203. clnt_sperrno(stat)
  204.     enum clnt_stat stat;
  205. {
  206.     int i;
  207.  
  208.     for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
  209.         if (rpc_errlist[i].status == stat) {
  210.             return (rpc_errlist[i].message);
  211.         }
  212.     }
  213.     return ("RPC: (unknown error code)");
  214. }
  215.  
  216. void
  217. clnt_perrno(num)
  218.     enum clnt_stat num;
  219. {
  220.     (void) fprintf(stderr,"%s",clnt_sperrno(num));
  221. }
  222.  
  223.  
  224. char *
  225. clnt_spcreateerror(s)
  226.     char *s;
  227. {
  228.     char *str = _buf();
  229.  
  230.     if (str == 0)
  231.         return(0);
  232.     (void) sprintf(str, "%s: ", s);
  233.     (void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
  234.     switch (rpc_createerr.cf_stat) {
  235.     case RPC_PMAPFAILURE:
  236.         (void) strcat(str, " - ");
  237.         (void) strcat(str,
  238.             clnt_sperrno(rpc_createerr.cf_error.re_status));
  239.         break;
  240.  
  241.     case RPC_SYSTEMERROR:
  242.         (void) strcat(str, " - ");
  243.         (void) strcat(str, strerror(rpc_createerr.cf_error.re_errno));
  244.         break;
  245.     }
  246.     (void) strcat(str, "\n");
  247.     return (str);
  248. }
  249.  
  250. void
  251. clnt_pcreateerror(s)
  252.     char *s;
  253. {
  254.     (void) fprintf(stderr,"%s",clnt_spcreateerror(s));
  255. }
  256.  
  257. struct auth_errtab {
  258.     enum auth_stat status;    
  259.     char *message;
  260. };
  261.  
  262. static struct auth_errtab auth_errlist[] = {
  263.     { AUTH_OK,
  264.         "Authentication OK" },
  265.     { AUTH_BADCRED,
  266.         "Invalid client credential" },
  267.     { AUTH_REJECTEDCRED,
  268.         "Server rejected credential" },
  269.     { AUTH_BADVERF,
  270.         "Invalid client verifier" },
  271.     { AUTH_REJECTEDVERF,
  272.         "Server rejected verifier" },
  273.     { AUTH_TOOWEAK,
  274.         "Client credential too weak" },
  275.     { AUTH_INVALIDRESP,
  276.         "Invalid server verifier" },
  277.     { AUTH_FAILED,
  278.         "Failed (unspecified error)" },
  279. };
  280.  
  281. static char *
  282. auth_errmsg(stat)
  283.     enum auth_stat stat;
  284. {
  285.     int i;
  286.  
  287.     for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
  288.         if (auth_errlist[i].status == stat) {
  289.             return(auth_errlist[i].message);
  290.         }
  291.     }
  292.     return(NULL);
  293. }
  294.